<template>
  <div class="goods-container">
    
    <div class="list">

      <!-- 使用js代码跳转设置点击事件，不用router-link生产的a链接跳转 -->
      <div class="item" v-for="item in goodsList" :key="item.id" @click="goGoodsInfo(item.id)"> 
        <img :src="item.img_url" />
        <h2 class="title">{{ item.title }}</h2>
        <p class="price">
          <span class="new">{{ item.sell_price }}</span>
          <span class="old">{{ item.market_price }}</span>
        </p>
        <div class="subtitle">
          <span>热卖中</span>
          <span>剩余{{ item.stock_quantity }}件</span>
        </div>
      </div>
    </div>
    <div class="button">
      <el-button type="danger" @click="getMore">加载更多</el-button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      pageIndex: 1,

      goodsList: [],
    };
  },

  created() {
    this.getGoodsData();
  },

  methods: {
    getGoodsData() {
      this.$http
        .get("api/getgoods?pageindex=" + this.pageIndex)
        .then((result) => {
          if (result.body.status === 0) {
            this.goodsList = this.goodsList.concat(result.body.message);
          }
        });
    },

    getMore() {
      this.pageIndex++;

      this.getGoodsData();
    },

    goGoodsInfo(id){

      //跳转方式有两种； 1.使用a链接跳转  2.使用js代码断跳转

      //js跳转的第一种方法
      // this.$router.push("/home/goodsinfo/" + id)

      //js跳转的第二种方法
      // this.$router.push({ path : "/home/goodsinfo/" + id})

      //js跳转的第三种方法，name需要价router.js种设置
      this.$router.push({ name : "goodsinfo", params : {id}})

      //js跳转的第四种方法，query带?的参数 /register?plan=213
      // this.$router.push({ path : "register", query : {id}})

    }
  },
};
</script>

<style lang="scss" scoped>
.goods-container {
  margin-bottom: 40px;

  .list {
    display: flex;
    flex-wrap: wrap;
    padding: 8px;
    justify-content: space-between;

    .item {
      width: 49%;
      margin: 6px 0;
      border: 1px solid #ccc;
      background: white;
      padding: 0 1px;
      box-shadow: 0 0 4px gray;
      min-height: 290px;

      img {
        width: 100%;
      }

      h2 {
        font-size: 12px;
        padding: 5px 0;
        margin-top: 0;
      }

      .price {
        font-size: 16px;
        color: lightcoral;
        margin-bottom: 5px;

        .old {
          color: lightgrey;
          text-decoration: line-through;
          margin-left: 10px;
        }
      }

      .subtitle {
        font-size: 12px;
        color: gray;
        display: flex;
        justify-content: space-between;
      }
    }
  }

  .button {

    margin: 0 10px;

    .el-button {
      padding: 0;
      margin: 0;
      width: 100%;
      height: 40px;
    }
  }
}
</style>